package com.ikun.core.utils;


import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class PageResult<T> {

    /**
     * 总记录数
     **/
    private Long total;

    /**
     * 当前页码
     **/
    private Integer current;

    /**
     * 每页条数
     **/
    private Integer size;

    /**
     * 扩展字段
     **/
    private Map<String, Object> extend;

    /**
     * 数据列表
     **/
    private List<T> lists;

/*
    /**
     * PageHelper分页
     *
     * @param list (分页数据集)
     * @param <T>  (泛型)
     * @return PageList
     * @author fzr
     * /
    public static <T> PageResult<T> pageHelper(List<T> list) {
        PageResult<T> pageResult = new PageResult<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setCurrent(pageInfo.getPageNum());
        pageResult.setSize(pageInfo.getPageSize());
        pageResult.setLists(pageInfo.getList());
        return pageResult;
    }

    /**
     * PageHelper分页(数据额外处理)
     *
     * @param list (分页数据集)
     * @param <T>  (泛型)
     * @return PageList
     * @author fzr
     * /
    public static <T> PageResult<T> pageHelper(List<T> list, List<T> data) {
        PageResult<T> pageResult = new PageResult<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setCurrent(pageInfo.getPageSize());
        pageResult.setSize(pageInfo.getPageNum());
        pageResult.setLists(data);
        return pageResult;
    }
*/

    /**
     * MyBatisPlus分页
     *
     * @param iPage (分页)
     * @param <T>   (泛型)
     * @return PageList
     * @author fzr
     */
    public static <T> PageResult<T> iPageHandle(IPage<T> iPage) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setTotal(iPage.getTotal());
        pageResult.setCurrent((int) iPage.getCurrent());
        pageResult.setSize((int) iPage.getSize());
        pageResult.setLists(iPage.getRecords());
        return pageResult;
    }

    /**
     * MyBatisPlus分页(数据额外处理)
     *
     * @param total  (总条数)
     * @param pageNo (当前页码)
     * @param size   (每页条数)
     * @param list   (列表数据)
     * @param <T>    (泛型)
     * @return PageList
     * @author fzr
     */
    public static <T> PageResult<T> iPageHandle(Long total, Long pageNo, Long size, List<T> list) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setCurrent(Math.toIntExact(pageNo));
        pageResult.setSize(Math.toIntExact(size));
        pageResult.setLists(list);
        return pageResult;
    }

    /**
     * MyBatisPlus分页(数据额外处理)
     *
     * @param total  (总条数)
     * @param pageNo (当前页码)
     * @param size   (每页条数)
     * @param list   (列表数据)
     * @param extend (扩展字段)
     * @param <T>    (泛型)
     * @return PageResult<T>
     */
    public static <T> PageResult<T> iPageHandle(Long total, Long pageNo, Long size, List<T> list, Map<String, Object> extend) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setCurrent(Math.toIntExact(pageNo));
        pageResult.setSize(Math.toIntExact(size));
        pageResult.setLists(list);
        pageResult.setExtend(extend);
        return pageResult;
    }

}


